<cfcomponent>

	<cfscript>
		this.name = "WMS_Admin";
		this.clientManagement = true;
		this.datasource = "WMS";
		this.clientStorage = "WMS";
		this.loginStorage = "cookie";
		this.applicationTimeout = CreateTimeSpan(100,0,0,0);
		// this.mappings["/assets"] = "C:\inetpub\assets";
		// this.mappings["/validateThis"] = "C:\inetpub\assets\validateThis";
		// StructInsert(this.mappings, "/validateThis", "C:\inetpub\assets\validateThis");
	</cfscript>
	
	<cffunction name="onApplicationStart">
		<cfscript>
			if (cgi.HTTP_HOST eq "www.calgrants.org" or cgi.HTTP_HOST eq "calgrants.org"){
				application.mode = "prod";
			} else {
				application.mode = "dev";
			}
			application.pathToApplication = "/applications/admin";
			application.pathToJQuery = "/assets/jquery/jquery.min.js";
			application.pathToJQueryUI = "/assets/jquery/jquery-ui.min.js";
			application.pathToKendo = "/assets/kendoui/js/kendo.all.min.js";
			application.pathToKendoDefaultCSS = "/assets/kendoui/styles/kendo.default.min.css";
			application.pathToKendoCommonCSS = "/assets/kendoui/styles/kendo.common.min.css";
			application.pathToBootstrap = "/assets/bootstrap/js/bootstrap.min.js";
			application.pathToBootstrapCSS = "/assets/bootstrap/css/bootstrap-combined.no-icons.min.css";
			application.pathToFontAwesomeCSS = "/assets/font-awesome/css/font-awesome.min.css";
			application.pathToComponents = "assets.calgrants.com";
			application.pathToBO = application.pathToComponents & ".bo";
			application.pathToServices = application.pathToComponents & ".service";
			application.pathToGateways = application.pathToComponents & ".gateway";
			application.pathToUI = "/assets/calgrants/ui";
			application.pathToTemplates = "/assets/calgrants/ui/templates";
			application.pathToTemp = "/assets/calgrants/temp/";
			application.tempDirectory = ExpandPath(GetDirectoryFromPath(application.pathToTemp));
			//application.bootstrapCDNUrl = "//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js";
			//application.bootstrapCSSCDNUrl = "//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css";
			//application.jQueryCDNUrl = "//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
			//application.jQueryUICDNUrl = "//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js";
			//application.kendoCDNUrl = "//da7xgjtj801h2.cloudfront.net/2012.2.710/js/kendo.all.min.js";
			//application.kendoCommonCSSCDNUrl = "//cdn.kendostatic.com/2012.2.710/styles/kendo.common.min.css";
   			//application.kendoDefaultCSSCDNUrl = "//cdn.kendostatic.com/2012.2.710/styles/kendo.default.min.css";
			application.apiUrl = "/assets/calgrants/com/API.cfc";
			application.wrapperUrl = "/wseval/";
			application.MAILCHIMP_API_KEY = "20ec622a3d9c4509d036eb90c2c5c07c-us2";
			application.MAILCHIMP_SERVICE_URL = "http://us2.api.mailchimp.com/1.2/";
			// output format can be xml or json.  use lowercase values
			application.MAILCHIMP_OUTPUT_FORMAT = "xml";
			application.MAILCHIMP_LISTID = "fd5afdef10";
			application.personality = {};
			application.personality.name = "Cash for College";
			application.personality.email = "cashforcollege@csac.ca.gov";
			application.personality.bcc = "cash4collegeca@gmail.com";
			application.personality.phone = "916-464-6410";
			application.developer = {};
			application.developer.name = "Syntric";
			application.developer.email = "support@syntric.com";
			application.developer.phone = "916-776-6650";
			application.fulfillment = {};
			application.fulfillment.name = "ECMC";
			application.fulfillment.email = "publications@ecmc.org";
			application.payment = {};
			application.payment.name = "Los Angeles Area Chamber of Commerce";
			application.payment.email = "cashforcollege@lachamber.com";
			application.currentProgramYear = "2014-2015";
			application.initDateTime = now();
			initValidator();
		</cfscript>
	</cffunction>
	
	<cffunction name="onRequestStart">
	
		<cfif cgi.HTTPS eq "off">
			<cflocation url="https://#cgi.SERVER_NAME##cgi.SCRIPT_NAME#" addtoken="no">
		</cfif>
	
		<cfset request.alerts = ArrayNew(1) />
	
		<cfif IsDefined("initApp")>
			<cflogout />
			<cfscript>
				onApplicationStart();
			</cfscript>
		</cfif>

        <cfif IsDefined("initValidator")>
            <cfscript>
                initValidator();
            </cfscript>
        </cfif>
		
		<cflock scope="application" timeout="20" type="readonly">
			<cfset StructAppend(request, application) />
		</cflock>
		
		<cfif IsDefined("logout")>
			<cflogout />
		</cfif>
		
		<!--- <cfif IsDefined("clearClient")>
			<cfloop index="cVar" list="#GetClientVariablesList()#">
				<cfset DeleteClientVariable(cVar) />
			</cfloop>
		</cfif> --->
		
		<cfscript>
			var storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
		</cfscript>

		<cflogin>
			<cfif IsDefined("cflogin") and structKeyExists(cflogin, "name") and structKeyExists(cflogin, "password")>
			
				<cfscript>
					var authResult = storedProcedureGateway.call(sp = "authUser", User_ID = "", Email = cflogin.name, Password = cflogin.password, Program_Year = request.currentProgramYear);
				</cfscript>

				<cfif authResult.RecordCount eq 1 AND authResult.Approved AND NOT authResult.Archived>
					<cfif authResult.Role eq "Super User" or authResult.Role eq "Regional Admin">
						<cfloginuser name="#authResult.User_ID#" password="" roles="#authResult.Role#" />
					<cfelse>
						<cflocation url="/applications/workshops/?logout" addtoken="no" />
						<cfabort>
					</cfif>
				<cfelseif authResult.RecordCount eq 1 AND NOT authResult.Approved>
					<cfset ArrayAppend(request.alerts, "Account found but not approved.  Please contact Cash for College to approve your account.") />
				<cfelseif authResult.RecordCount eq 0>
					<cfset ArrayAppend(request.alerts, "Login failed.  Check your E-mail and Password.") />
				<cfelseif authResult.RecordCount gt 1>
					<cfset ArrayAppend(request.alerts, "Duplicate accounts found for #cflogin.name#.  Please contact Cash for College to resolve this issue.") />
				</cfif>
			</cfif>
		</cflogin>

		<cfif NOT IsUserLoggedIn()>
			<cfinclude template="login.cfm" />
			<cfabort>
		<cfelse>
			<cfscript>
				var authUser = storedProcedureGateway.call(sp = "authUser", User_ID = GetAuthUser(), Email = "", Password = "", Program_Year = request.currentProgramYear);
			</cfscript>
			<cfif authUser.RecordCount eq 1 AND authUser.Approved AND NOT authUser.Archived>
				<cfscript>
					request.authUser.lastVisit = Now();
					request.authUser.organizationID = authUser.Organization_ID;
					request.authUser.organization = authUser.Organization;
					request.authUser.paID = authUser.PA_ID;
					request.authUser.oapID = authUser.OAP_ID;
					request.authUser.regionID = authUser.Region_IDs;
					request.authUser.userID = authUser.User_ID;
					request.authUser.fullName = authUser.First_Name & " " & authUser.Last_Name;
					request.authUser.email = authUser.Email;
					request.authUser.role = authUser.Role;
				</cfscript>
			<cfelseif authUser.RecordCount eq 1 AND NOT authUser.Approved>
				<cfset ArrayAppend(request.alerts, "Account found but not approved.  Please contact Cash for College to approve your account.") />
				<cfinclude template="login.cfm" />
				<cfabort>
			<cfelseif authUser.RecordCount eq 0>
				<cfset ArrayAppend(request.alerts, "Login failed.  Check your E-mail and Password.") />
				<cfinclude template="login.cfm" />
				<cfabort>
			<cfelseif authUser.RecordCount gt 1>
				<cfset ArrayAppend(request.alerts, "Duplicate accounts found for #cflogin.name#.  Please contact Cash for College to resolve this issue.") />
				<cfinclude template="login.cfm" />
				<cfabort>
			</cfif>
		</cfif>
	
	</cffunction>
	
	<cffunction name="onError">
		<cfargument name="Exception" required="true" />
		<cfargument name="EventName" required="true" />
		<cfif application.mode eq "prod">
			<cfmail to="kenhobbs@gmail.com" from="kenhobbs@gmail.com" subject="Admin Error" type="html">
				<cfdump var="#arguments.Exception#" expand="yes" label="Exception" />
				<cfdump var="#cgi#" expand="yes" label="CGI" />
				<cfdump var="#form#" expand="yes" label="Form" />
				<cfdump var="#url#" expand="yes" label="URL" />
				<cfdump var="#request#" expand="yes" label="request" />
			</cfmail>
			<h1>Ooops</h1>
			<p>The application has encountered an error.  An error message has been sent to the developer.</p>
			<p>If you require immediate assistance with this error, please <a href="mailto:support@syntric.com">contact the developer</a>.</p>
		<cfelse>
			<cfif IsDefined("arguments.Exception")>
				<cfdump var="#arguments.Exception#" expand="no" label="Exception" />
			</cfif>
			<cfdump var="#cgi#" expand="no" label="CGI" />
			<cfdump var="#form#" expand="no" label="Form" />
			<cfdump var="#url#" expand="no" label="URL" />
			<cfdump var="#request#" expand="no" label="request" />
		</cfif>
		<cfabort>
	</cffunction>
	
	<cffunction name="onRequestEnd">
        <cfsetting showdebugoutput="no">
    </cffunction>

    <cffunction name="initValidator">
        <cfscript>
            lock scope="application" timeout="20" type="readonly"
            {
                var validatorConfig = {extraRuleValidatorComponentPaths = "/assets/calgrants/com/validators/", definitionPath = "/assets/calgrants/com/bo/"};
                application.validator = new validateThis.ValidateThis(validatorConfig);
            }
        </cfscript>
    </cffunction>

</cfcomponent>